require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../libs/iconfont/iconfont/iconfont.css')
const axios = require('axios')
require('weui')
const weui = require('weui.js')
require('../css/information.less')

//新用户
axios.get('http://139.9.177.51:8099/users/accountinfo',{
    params:{
        userId:localStorage.userId
    }
}).then(function(res){
    let data=res.data.data
    console.log(data);
    //昵称
    nickname.value=data.nickname
    //头像
    if(data.imgurl){
        image.src=data.imgurl
    }
    //性别
    if(data.gender){
        sex.textContent=data.gender
    }
    //生日
    if(data.birthday){
        birthday.textContent=data.birthday.substr(0,10)
    }
    //省份城市
    if(data.address){
        city.textContent=data.address.split(',')[0]
        subcity.textContent=data.address.split(',')[1]
    }
    //个性签名
    if(data.sign){
        area.value=data.sign
        num.textContent=area.value.length
    }
})
//渲染字体数量
const num=document.querySelector('#num')
const area=document.querySelector('#area')
area.addEventListener('input',function(){
    num.textContent=area.value.length
})

//单个选择器-性别
document.querySelector('#sexPicker').addEventListener('click', function () {
    weui.picker([{
        label: '男',
        value: 0
    }, {
        label: '女',
        value: 1
    }], {

        onConfirm: function (result) {
            //指。result0数组下的label对象
            //console.log(result[0].label);
            //渲染
            document.querySelector('#sex').textContent = result[0].label
        },
        title: '请选择性别'
    });
});



//补零
function zero(n) {
    return n < 9 ? '0' + n : n
}
//多列选择器-生日
document.querySelector('#birthdayDatePicker').addEventListener('click', function () {
    weui.datePicker({
        //开始时间（从现在时间开始减80年
        start: new Date().getFullYear() - 80,

        end: new Date().getFullYear(),

        onConfirm: function (result) {
            //定义数组arr，包含年月日
            let arr = [result[0].value, zero(result[1].value), zero(result[2].value)]
            //截取对象改为-
            arr.join('-')
            //console.log( arr.join('-'));

            //渲染
            document.querySelector('#birthday').textContent = arr.join('-')
        },
        title: '请选择日期'
    });
});











//头像选择
//获取元素
const image = document.querySelector('#image')
const int = document.querySelector('#int')
//改变
int.addEventListener('change', function () {
    //console.log(int.files[0]);
    //new一个formdata参数类型数组
    let fd = new FormData()
    //将参数和对象添加进去
    fd.append('imgurl', int.files[0])
    console.log(fd);
    //后台渲染axios
    axios.post('http://139.9.177.51:8099/users/upload', fd).then(function (res) {

        //console.log(res);
        //如果status等于1
        if (res.data.status == 1) {
            //弹出警示框
            weui.toast('文件格式有误')
        } else {
            //console.log(res);
            //否则更改图片路径：服务器地址+res.data.data下的地址
            image.src = 'http://139.9.177.51:8099' + res.data.data
        }
    })
})

//点击图片时，int上的点击更改转移到点击图片上
image.addEventListener('click', function () {
    int.click()
})



//城市
let arrobb=[]
//省份
let arrobj = []
axios.get('http://139.9.177.51:8099/address/province').then(function (res) {
    //console.log(res.data.data);
    let arr = res.data.data
    arrobj = arr.map(function (i) {
        //onsole.log(i);
        return {
            label: i.name,
            value: i.addressId
        }

    })
    //console.log(arrobj);
})



// let result = ''
//单个选择器-省份
document.querySelector('#cityPicker').addEventListener('click', function () {
    weui.picker(arrobj, {

        onConfirm: function (result) {
            //指。result0数组下的label对象
            //console.log(result[0].label);
            //渲染
            document.querySelector('#city').textContent = result[0].label
            //城市-关联
            let pid = result[0].value
            // console.log(pid);
            axios.get('http://139.9.177.51:8099/address/city/' + pid).then(function (res) {
                //console.log(res.data.data);
                let arr = res.data.data
                arrobb = arr.map(function (i) {
                    return {
                        label: i.name,
                        value: i.addressId
                    }
                })
                //自动选择第一个
                subcity.textContent=arrobb[0].label
            })

        },
        title: '请选择城市'
    });
});



//单个选择器-城市
document.querySelector('#subcityPicker').addEventListener('click', function () {
    weui.picker(arrobb, {

        onConfirm: function (result) {
            //指。result0数组下的label对象
            //console.log(result[0].label);
            //渲染
            document.querySelector('#subcity').textContent = result[0].label
        },
        title: '请选择省份'
    });
});



//提交
const btn = document.querySelector('#btn')

//console.log(params);
btn.addEventListener('click', function () {
    let params = {
        imgurl: document.querySelector('#image').src,
        nickname: document.querySelector('#nickname').value,
        gender: document.querySelector('#sex').textContent,
        birthday: document.querySelector('#birthday').textContent,
        address: [document.querySelector('#city').textContent, document.querySelector('#subcity').textContent],
        sign: document.querySelector('#area').value,
        userId: localStorage.getItem('userId')
    }
console.log(params);
    axios.post('http://139.9.177.51:8099/users/userEdit', params).then(function (res) {
        // console.log(res.data);
        if (res.data.status == 0) {
            weui.toast('修改成功', {
                callback: function () {
                    location.href = './my.html'
                }
            })

        } else {
            console.log(res);
            weui.alert('修改失败')
        }
    })
})




